package br.com.robot.implement.nuuvem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import br.com.model.SeedsToNavigate;
import br.com.model.dao.DAOFactory;
import br.com.robot.RobotBean;
import br.com.robot.RobotConfigurationBasic;
import br.com.robot.RobotController;
import br.com.robot.RobotList;
import br.com.robot.implement.AbstractRobot;
import br.com.util.ER;
import br.com.util.Web;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;

public class Nuuvem extends AbstractRobot
{
	/**
	 * Key para unir os Robos
	 */
	public static final String KEY_SEEDS = "nuuvemseeds";
	
	private List<String> setLst = new ArrayList<String>( );
	
	@Override
	public void onStart( )
	{
		super.onStart( );
		
		setApenasPaginas( );
	}
	
	@Override
	public boolean shouldVisit( WebURL url ) 
	{
		return super.shouldVisit( url );
	}

	/**
	 * This function is called when a page is fetched and ready to be processed
	 * by your program.
	 */
	@Override
	public void visit( Page page )
	{
		String url = page.getWebURL( ).getURL( );
		
		System.out.println( "URL: " + url );

		if( page.getParseData( ) instanceof HtmlParseData ) 
		{
			List<WebURL> links = ((HtmlParseData)page.getParseData( )).getOutgoingUrls( );

			for( WebURL webURL : links )
			{
				if( webURL.getURL( ).matches( "http://www.nuuvem.com.br/produto/.*$" ) )
				{
					setLst.add( webURL.getURL( ) );
				}
			}
		}
	}

	@Override
	public List<String> configuraSeeds( )
	{
		List<String> lst = new ArrayList<String>( );
		
		try
		{
			String buscaExprecao = ER.buscaExprecao( Web.novaInstacia( ).get( "http://www.nuuvem.com.br/produtos?page=1" ), 
					 "<li class=\"last\"><a href=\"http://www.nuuvem.com.br/produtos\\?page=(.*?)\">.*?</" );

			int totalPaginas = Integer.parseInt( buscaExprecao );
			
			for( int i = 1; i <= totalPaginas; i++ )
				lst.add( "http://www.nuuvem.com.br/produtos?page=" + i );
		} 
		catch( Exception e )
		{
			e.printStackTrace( );
		}
		
		return lst;
	}
	
	@Override
	public void onBeforeExit( )
	{
		super.onBeforeExit( );
		
		try
		{
			List<SeedsToNavigate> lst = new ArrayList<SeedsToNavigate>( );
			
			for( String url : setLst )
			{
				SeedsToNavigate seed = new SeedsToNavigate( );
				
				seed.setRobo( infBean( ).getId( ) + "" );
			 	seed.setUrl ( url                      );
			 	
				lst.add( seed );
			}
			
			DAOFactory.infDao( ).save( lst );
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
	}

	@Override
	public RobotConfigurationBasic infConfig( )
	{
		RobotConfigurationBasic configs = new RobotConfigurationBasic( )
			.setFollowRedirect  ( true                        )
			.setNumeroCrawllersm( 100                         )
			.setProfundidade    ( 0                           )
			.setRobots          ( Arrays.asList( infBean( ) ) );
		
		return configs;
	}

	@Override
	public RobotBean<? extends AbstractRobot> infBean( )
	{
		return RobotList.getBean( RobotList.ID_NUUVEM_ROBOT );
	}

	@Override
	public void onFinalize( )
	{
		RobotController.infController( ).executa( Nuuvem_Games.class );
	}
}